﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class AquacultureKebieManageControl : YuanlinUserControl,
        IReplyConverter<AquacultureKebieReply, GetListOperation>,
        IReplyConverter<AquacultureReply, GetOperation>
    {
        protected CheckBoxList kebieCheckBoxList;//科别CheckBox列表
        protected IButton SaveButton;
        private EntityList aquacultureKebies;
        private Aquaculture aquaculture;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetAquacultureKebieListRequest(this));
            requestBinders.Add(RequestHelper.BuildGetAquacultureRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
        }

        private void Bind()
        {
            this.kebieCheckBoxList.DataSource = aquacultureKebies;
            this.kebieCheckBoxList.DataTextField = "Title";
            this.kebieCheckBoxList.DataValueField = "EntityId";
            this.kebieCheckBoxList.DataBind();

            if (aquaculture != null)
            {
                foreach (ListItem item in this.kebieCheckBoxList.Items)
                {
                    if (item.Value == aquaculture.KebieId.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            string kebie = null;
            foreach (ListItem item in this.kebieCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    kebie = item.Value;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(kebie))
            {
                aquaculture.KebieId.Value = Globals.ChangeType<int>(kebie);
                BusinessManager.UpdateAquaculture(aquaculture);

                Globals.ShowMessage(Page, "SaveAquacultureKebie", "修改成功。");
            }

        }

        #region IReplyConverter<AquacultureKebieReply,GetListOperation> Members

        public void ConvertReply(AquacultureKebieReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                aquacultureKebies = reply.EntityList;
            }
        }

        #endregion

        #region IReplyConverter<AquacultureReply,GetOperation> Members

        public void ConvertReply(AquacultureReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                aquaculture = reply.Entity as Aquaculture;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}